package models;

import java.util.List;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
@DiscriminatorValue("advertisement")
public class Advertisement extends Product {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1923361971474833433L;

	public static Finder<Long, Advertisement> find = new Finder<Long, Advertisement>(
			Long.class, Advertisement.class);

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "user_id", nullable = true)
	private User user;

	public static void delete(Long id) {
		// find.ref(id).setUser(null); // TODO hiba ... faszom ki van vele.
		find.ref(id).delete();
	}

	public static List<Advertisement> adsByUser(User u) {
		return find.where().eq("user_id", u.getId()).findList();
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((user == null) ? 0 : user.hashCode());
		return result; // TODO getterekre
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!super.equals(obj)) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Advertisement other = (Advertisement) obj;
		if (user == null) {
			if (other.user != null) {
				return false;
			}
		} else if (!user.equals(other.user)) {
			return false;
		}
		return true;
	}

}
